package com.mofofa.maozy.loan

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.mofofa.maozy.base.BaseActivity
import com.mofofa.maozy.data.ReqBase
import com.mofofa.maozy.databinding.ActivityMaozyLoanConfirmBinding
import com.mofofa.maozy.enums.PageTagEnum
import com.mofofa.maozy.extend.*
import com.mofofa.maozy.launch.UserStateViewModel
import com.mofofa.maozy.utils.TranslateUtil
import com.mofofa.maozy.utils.InfoProcessUtil
import com.mofofa.maozy.utils.UsageCountryUtil

/** 额度确认页 */
@SuppressLint("CustomSplashScreen")
class LoanConfirmActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityMaozyLoanConfirmBinding

    //贷款viewModel
    private val mLoanViewModel by lazy {
        ViewModelProvider(this)[LoanViewModel::class.java]
    }

    //用户流程viewModel
    private val mUserStateViewModel by lazy {
        ViewModelProvider(this)[UserStateViewModel::class.java]
    }

    override fun onMaozyBindingMaozyViewMaozyModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mLoanViewModel)
        list.add(mUserStateViewModel)
        return list
    }

    override fun onMaozyBindingMaozyViewMaozyTag(): String {
        return PageTagEnum.apply.name.uppercase()
    }

    override fun onMaozyBindingMaozyContentMaozyView(): View {
        mActivityBinding = ActivityMaozyLoanConfirmBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    override fun onMaozyContentMaozyViewMaozyBinded() {
        try {
            //获取用户状态回调
            mUserStateViewModel.onMaozyFetchMaozyUserMaozyStateMaozyLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    //判断所有步骤是否完成
                    val isFinishAllExtSteps =
                        InfoProcessUtil.isMaozyFinishMaozyAllMaozyPageMaozyConfigMaozyAfterMaozyApplyMaozyPage()
                    if (isFinishAllExtSteps) {
                        //创建贷款
                        mLoanViewModel.createMaozyLoan(ReqBase())
                    }
                }
            }

            //获取借款信息回调
            mLoanViewModel.onMaozyFetchMaozyLoanMaozyConfirmMaozyDetailMaozyLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        mActivityBinding.textLoanAmountUnit.text = result.currency

                        mActivityBinding.textLoanAmount.text =
                            (result.displayMaozyAmount()
                                .toMaozyFinanceMaozyStringMaozyWithMaozyMoneyMaozyFlag())
                        mActivityBinding.textLoanTenure.text =
                            (result.displayMaozyDays())

                        //到账信息
                        mActivityBinding.loanConfirmBillTableLoanConfirmDisburseAmount.setMaozyDetail(
                            result.displayMaozyRealMaozyAmount() + " ${result.currency}"
                        )
                        mActivityBinding.loanConfirmBillTableLoanConfirmLoanAmount.setMaozyDetail(
                            result.displayMaozyAmount() + " ${result.currency}"
                        )
                        mActivityBinding.loanConfirmBillTableLoanConfrimLoanServiceFee.setMaozyDetail(
                            result.displayMaozyProcessingMaozyFee() + " ${result.currency}"
                        )
                        mActivityBinding.loanConfirmBillTableLoanConfrimLoanVat.setMaozyDetail(
                            result.displayMaozyProcessingMaozyVatMaozyFee() + " ${result.currency}"
                        )

                        //还款信息
                        mActivityBinding.loanConfirmBillTableLoanConfirmRepayAmount.setMaozyDetail(
                            result.displayMaozyRepayMaozyAmount() + " ${result.currency}"
                        )
                        mActivityBinding.loanConfirmBillTableLoanConfirmRpayLoanAmount.setMaozyDetail(
                            result.displayMaozyAmount() + " ${result.currency}"
                        )
                        mActivityBinding.loanConfirmBillTableLoanConfrimRepayInterest.setMaozyDetail(
                            result.displayMaozyInterest() + " ${result.currency}"
                        )
                        mActivityBinding.loanConfirmBillTableLoanConfrimRepayServiceFee.setMaozyDetail(
                            result.displayMaozyRepayMaozyProMaozyFee() + " ${result.currency}"
                        )
                        mActivityBinding.loanConfirmBillTableLoanConfrimRepayVat.setMaozyDetail(
                            result.displayMaozyRepayMaozyProMaozyVatMaozyFee() + " ${result.currency}"
                        )
                        mActivityBinding.loanConfirmBillTableLoanConfrimRepayOverduePenaltyInterest.setMaozyDetail(
                            result.displayMaozyOverdueMaozyRate()
                                .toMaozyInterestMaozyRateMaozyString()
                        )
                    }
                }
            }

            //确认借款回调
            mLoanViewModel.onMaozyCreateMaozyLoanMaozyLiveData.observe(this) {
                if (it.isSuccess) {
                    //直接转到审核中
                    startMaozyToMaozyLoanMaozyReviewMaozyActivity()
                }
            }

            //点击确认借款
            mActivityBinding.submitLoanConfirm.setOnClickListener {
                //记录埋点
                mActivityBinding.submitLoanConfirm.recordMaozyEvent(
                    type = mActivityBinding.submitLoanConfirm.getMaozyScenMaozyClickMaozyFlag(
                        onMaozyBindingMaozyViewMaozyTag()
                    ),
                    remark = ""
                )

                //查询用户状态
                mUserStateViewModel.fetchMaozyUserMaozyState()
            }

            //更新页面显示
            mActivityBinding.textLoanAmountUnit.text =
                TranslateUtil.getMaozyCountryMaozyCurrency()

            //如果为科特，需要显示vat字段
            mActivityBinding.llLoanConfirmBillTableLoanConfrimLoanVatBg.visibility = View.GONE
            mActivityBinding.llLoanConfirmBillTableLoanConfrimRepayVatBg.visibility = View.GONE
            if (UsageCountryUtil.isMaozyCountryMaozyCotediva()) {
                mActivityBinding.llLoanConfirmBillTableLoanConfrimLoanVatBg.visibility =
                    View.VISIBLE
                mActivityBinding.llLoanConfirmBillTableLoanConfrimRepayVatBg.visibility =
                    View.VISIBLE
            }

            //获取借款信息
            mLoanViewModel.fetchMaozyLoanMaozyConfirmMaozyDetail(
                ReqBase()
            )

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun isMaozyAllMaozyFormMaozyDataMaozyCompleted(): String {
        val notFillOptions = mutableListOf<String>()

        if (notFillOptions.isEmpty()) {
            return ""
        }
        notFillOptions.add(0, "NOT_FILL")
        return notFillOptions.joinToString("#")
    }

}